package com.yingdong.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class ExcelTemplateGenerator {
	
	
	public static void main(String[] args) {
		// 开始设置Freemarker
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("UTF-8");
		// 设置模板文件所在的目录
		cfg.setClassForTemplateLoading(ExcelTemplateGenerator.class, "/template");
		Writer out = null;
		try {
			// 取得模板文件
			Template t = cfg.getTemplate("ExcelTemplate.ftl");
			File[] files = new File("../ydserver/excel").listFiles();
			for (File file : files) {
				if (file.isFile() && file.getName().contains(".xlsx")) {
					String fileName = file.getName().split("\\.")[0];
					Map root = new HashMap();
					List fields = Excel2007Reader.readFieldsFromFile("../ydserver/excel/" + fileName + ".xlsx");
					root.put("fields", fields);
					root.put("classname", fileName);
					out = new FileWriter("src/main/java/com/yingdong/excel/" + fileName + ".java");
					t.process(root, out);
					out.close();
				}
			}
			

		} catch (Exception e1) {

			e1.printStackTrace();
		} finally {
			try {

			} catch (Exception e) {

				e.printStackTrace();
			}
		}

	}
}
